package icu.wdtt.mapper;

import icu.wdtt.model.Files;
import icu.wdtt.model.Role;
import icu.wdtt.model.User;
import org.apache.ibatis.annotations.*;
import org.springframework.context.annotation.Bean;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user WHERE uid=#{uid}")
    public User get(int uid);

    @Select("SELECT * FROM user")
    public List<User>  getAll();

    @Delete("DELETE FROM user WHERE uid=#{uid}")
    public void del(int uid);

    @Update("UPDATE user SET username=#{username},password=#{password},email=#{email} WHERE uid=#[id}")
    public void update(User user);

    @Options(useGeneratedKeys = true,keyProperty = "uid",keyColumn = "uid")
    @Insert("INSERT INTO user VALUES(null,#{username},#{password},#{email})")
    public void add(User user);

    @Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")
    public User login(@Param("username") String username ,@Param("password") String password);

    @Select("SELECT r.* FROM (SELECT * FROM user_role WHERE uid=#{uid} ) ur,role r WHERE r.rid=ur.rid")
    @Results(
            {
                    @Result(column = "rid" , property = "rid"),
                    @Result(column = "rname" , property = "rname"),
                    @Result(column = "rdescribe",property = "rdescribe"),
                    @Result(
                            property = "authorityList",
                            column = "rid" ,
                            javaType = List.class,
                            many = @Many(select = "icu.wdtt.mapper.RoleMapper.getRoleAndAuthority")
                    )
            }
    )
    public List<Role> getRoleListByUid(int uid);

    @Select("SELECT * FROM user")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "email",property = "email"),
            @Result(
                    property = "roleList",
                    column = "uid",
                    javaType = List.class,
                    many = @Many(select = "icu.wdtt.mapper.UserMapper.getRoleListByUid")
            )
    })
    public List<User> getUserAndRoleAll();

    @Select("SELECT * FROM user WHERE uid=#{uid}")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "email",property = "email"),
            @Result(
                    property = "roleList",
                    column = "uid",
                    javaType = List.class,
                    many = @Many(select = "icu.wdtt.mapper.UserMapper.getRoleListByUid")
            )
    })
    public User getUserAndRoleByUid(int uid);

    @Select("SELECT f.* FROM (SELECT * FROM user_files WHERE uid=#{uid}) uf,files f WHERE uf.fid=f.fid")
    public List<Files> getFilesListByUid(int uid);

    @Select("SELECT * FROM user")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "email",property = "email"),
            @Result(
                    property = "filesList",
                    column = "uid",
                    javaType = List.class,
                    many = @Many(select = "icu.wdtt.mapper.UserMapper.getFilesListByUid")
            )
    })
    public List<User> getUserAndFilesAll();


    @Select("SELECT * FROM user WHERE uid=#{uid}")
    @Results({
            @Result(column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "email",property = "email"),
            @Result(
                    property = "filesList",
                    column = "uid",
                    javaType = List.class,
                    many = @Many(select = "icu.wdtt.mapper.UserMapper.getFilesListByUid")
            )
    })
    public User getUserAndFilesByUid(int uid);

    @Insert("INSERT INTO user_role VALUES(#{uid},#{rid})")
    public void addUserAndRole(@Param("uid") int uid, @Param("rid") int rid);

    @Delete("DELETE FROM user_role WHERE uid=#{uid}")
    public void delUserAndRole(int uid);

    @Delete("DELETE FROM user_files WHERE uid=#{uid}")
    public void delUserAndFiles(int uid);

    @Select("SELECT * FROM user WHERE username=#{username}")
    public User getUserByUserName(@Param("username") String username);

    @Update("UPDATE user SET password = #{newPassword} WHERE uid=#{uid} AND password=#{oldPassword}")
    public void updatePassword(@Param("uid")int uid,@Param("oldPassword")String oldPassword ,@Param("newPassword")String newPassword);
}
